package com.share.im.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.share.im.R;
import com.share.im.activity.login.ILoginView;
import com.share.im.manager.XmppConnectionManager;
import com.share.im.model.LoginConfig;
import com.share.im.presenter.ILoginPresenter;
import com.share.im.presenter.LoginPresenter;
import com.share.im.util.common.StringUtil;
import com.share.im.util.common.ValidateUtil;
import com.share.im.util.ioc.annotation.InjectView;

/**
 * 登录
 * @author Administrator
 *
 */
public class LoginActivity extends ActivitySupport implements ILoginView
{
	@InjectView(id = R.id.ui_username_input)
	private EditText edt_username;
	@InjectView(id = R.id.ui_password_input)
	private EditText edt_pwd;
	@InjectView(id = R.id.remember)
	private CheckBox rememberCb;
	@InjectView(id = R.id.autologin)
	private CheckBox autologinCb;
	@InjectView(id = R.id.novisible)
	private CheckBox novisibleCb;

	@InjectView(id = R.id.ui_login_btn, click = "login")
	private Button btn_login = null;

	@InjectView(id = R.id.progress)
	private ProgressBar progressBar;

	private LoginConfig loginConfig;// 登陆配置

	private ILoginPresenter presenter;// 主导器

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		init();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		// 校验SD卡
		checkMemoryCard();
		// 检测网络和版本
		validateInternet();
		// 初始化xmpp配置
		XmppConnectionManager.getInstance().init(loginConfig);
	}

	/**
	 * 
	 * 初始化.
	 * 
	 */
	protected void init()
	{
		presenter = new LoginPresenter(this);
		loginConfig = getLoginConfig();
		// 如果为自动登录
		if (loginConfig.isAutoLogin())
		{
			login();
		}

		// 初始化各组件的默认状态
		edt_username.setText(loginConfig.getUsername());
		edt_pwd.setText(loginConfig.getPassword());
		rememberCb.setChecked(loginConfig.isRemember());
		rememberCb.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (!isChecked)
					autologinCb.setChecked(false);
			}
		});
		autologinCb.setChecked(loginConfig.isAutoLogin());
		novisibleCb.setChecked(loginConfig.isNovisible());
	}

	/**
	 * 
	 * 登录校验.
	 * 
	 * @return
	 */
	private boolean checkData()
	{
		boolean checked = false;
		checked = (!ValidateUtil.isEmpty(edt_username, "登录名") && !ValidateUtil.isEmpty(edt_pwd, "密码"));
		return checked;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.login_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		final EditText xmppHostText = new EditText(context);
		xmppHostText.setText(loginConfig.getXmppHost());
		switch (item.getItemId())
		{
		case R.id.menu_login_set:
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle("服务器设置").setIcon(android.R.drawable.ic_dialog_info).setView(xmppHostText).setMessage("请设置服务器IP地址").setPositiveButton("确定", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					String xmppHost = StringUtil.doEmpty(xmppHostText.getText().toString());
					loginConfig.setXmppHost(xmppHost);
					XmppConnectionManager.getInstance().init(loginConfig);
					LoginActivity.this.saveLoginConfig(loginConfig);
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog, int which)
				{
					dialog.cancel();// 取消弹出框
				}
			}).create().show();

			break;
		// case R.id.menu_relogin:
		// Intent intent = new Intent();
		// intent.setClass(context, LoginActivity.class);
		// startActivity(intent);
		// finish();
		// break;
		case R.id.menu_exit:
			isExit();
			break;
		}
		return true;

	}

	@Override
	public void onBackPressed()
	{
		isExit();
	}

	@Override
	public void showProgress()
	{
		progressBar.setVisibility(View.VISIBLE);
	}

	@Override
	public void hideProgress()
	{
		progressBar.setVisibility(View.GONE);
	}

	@Override
	public void navigateToHome(int result)
	{
		Intent intent = new Intent();
		if (loginConfig.isFirstStart())
		{// 如果是首次启动
			// intent.setClass(context, GuideViewActivity.class);
			intent.setClass(context, ContacterMainActivity.class);
			loginConfig.setFirstStart(false);
			showToast("首次登陆！");
		}
		else
		{
			showToast("登陆成功！");
			intent.setClass(context, ContacterMainActivity.class);
		}
		if (!loginConfig.isRemember())
		{
			loginConfig.setPassword("");// 不保存密码
		}
		loginConfig.setOnline(true);// 设置在线

		saveLoginConfig(loginConfig);// 保存用户配置信息
		startService(); // 初始化各项服务
		context.startActivity(intent);
	}

	/**
	 * 登陆事件
	 */
	public void login()
	{
		if (checkData() && validateInternet())
		{
			String username = edt_username.getText().toString();
			String password = edt_pwd.getText().toString();

			// 先记录下各组件的目前状态,登录成功后才保存
			loginConfig.setPassword(password);
			loginConfig.setUsername(username);
			loginConfig.setRemember(rememberCb.isChecked());
			loginConfig.setAutoLogin(autologinCb.isChecked());
			loginConfig.setNovisible(novisibleCb.isChecked());

			presenter.validateCredentials(loginConfig);
		}
	}

	@Override
	public void showMessage(String message)
	{
		showToast(message);
	}
}
